//
// Created by 李望轩 on 2020/11/17.
//

/*
 * 书籍增删改查操作
 */

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "LeWJson.h"
#include "LeRJson.h"
#include "../LeCJson/cJSON.h"


//从文件读入所有书籍信息
LeBookLink LeGetBookLinkFroFile() {

    LeBookLink Head, BookLinkList, t;
    //定义头节点
    Head = (LeBookLink) malloc(sizeof(BookStruct));//生成代插入节点
    Head->Next = NULL;
    t = Head;
    cJSON *root = NULL;//跟节点
    cJSON *Pitem1, *Pitem2;//临时变量
    cJSON *BookJsonList = LeReadJsonList(0, root);//获取Book数组列表
    int BookJsonListSize = cJSON_GetArraySize(BookJsonList);//获取数组长度
    //文件内容读出到结构体
    for (int i = 0; i < BookJsonListSize; i++) {
        BookLinkList = (LeBookLink) malloc(sizeof(BookStruct));//生成代插入节点
        Pitem1 = cJSON_GetArrayItem(BookJsonList, i);//根据下标读取内容
//        printf("item type is %d\n", Pitem1->type);//用于获取判断数据类型，这里不再判断

        Pitem2 = cJSON_GetObjectItem(Pitem1, "BID");
//        printf("Pitem2 type is %d\n", Pitem2->type);//用于获取判断数据类型，这里不再判断
        BookLinkList->BID = Pitem2->valueint;

        Pitem2 = cJSON_GetObjectItem(Pitem1, "BName");
        strcpy(BookLinkList->BName, Pitem2->valuestring);

        Pitem2 = cJSON_GetObjectItem(Pitem1, "BAName");
        strcpy(BookLinkList->BAName, Pitem2->valuestring);

        Pitem2 = cJSON_GetObjectItem(Pitem1, "BPID");
        BookLinkList->BPID = Pitem2->valueint;

        Pitem2 = cJSON_GetObjectItem(Pitem1, "BState");
        BookLinkList->BState = Pitem2->valueint;

        Pitem2 = cJSON_GetObjectItem(Pitem1, "SID");
        BookLinkList->SID = Pitem2->valueint;

        t->Next = BookLinkList;
        t = BookLinkList;
        BookLinkList = NULL;
    }
    t->Next = NULL;
    cJSON_Delete(root);
    return Head;

}

//获取当前书籍ID最大值
int LeGetMaxBookID(LeBookLink Head) {
    LeBookLink p = Head;
    int i = 66660000;
    while (p->Next) {
        p = p->Next;
        if (p->BID > i) {
            i = p->BID;
        }
    }
    return i;
}

//判断书本出版号是否存在
int LeGetBPIDExist(LeBookLink Head, long BPID) {
    LeBookLink p = Head;
    while (p->Next) {
        p = p->Next;
        if (p->BPID == BPID) {
            return 1;
        }
    }
    return 0;
}

//根据书籍出版号获取书籍名和作者名
void LeGetBNameANDBAName(LeBookLink Head, long BPID, char *BName, char *BAName) {
    LeBookLink p = Head;
    while (p->Next) {
        p = p->Next;
        if (p->BPID == BPID) {
            strcpy(BName, p->BName);
            strcpy(BAName, p->BAName);
            return;
        }
    }
}

//插入新书籍
void LeInsertBook(LeBookLink BookHead, LeStuLink StuHead, int BookID, long BPID, char *BName, char *BAName, int BState,
                  int SID) {

    //进行录入，并按照BID进行排序
    LeBookLink pre = BookHead, p;
    p = (LeBookLink) malloc(sizeof(BookStruct));//生成代插入节点
    p->BID = BookID;
    p->BPID = BPID;
    p->BState = BState;
    p->SID = SID;
    p->Next = NULL;
    strcpy(p->BName, BName);
    strcpy(p->BAName, BAName);

    //来到最后节点
    while (pre->Next) {
        pre = pre->Next;
    }
    pre->Next = p;
    p = pre = NULL;
    JsonWritFile(BookHead->Next, StuHead->Next);
}

//书籍序号查询
void LeGetBookInfoFroBID(LeBookLink BHead, int BID) {
    LeBookLink p = BHead->Next;
    while (p) {
        if (BID == p->BID) {
            printf("书籍ID：%d\t", p->BID);
            printf("书本名：%s\t", p->BName);
            printf("作者名：%s\t", p->BAName);
            printf("出版号：%ld\t", p->BPID);
            switch (p->BState) {
                case -1:
                    printf("书籍状态：不可用\n");
                    break;
                case 0:
                    printf("书籍状态：已借出\t");
                    printf("借书人ID：%d\n", p->SID);
                    break;
                case 1:
                    printf("书籍状态：可借出\n");
                    break;
            }
            return;
        }
        p = p->Next;
    }

}

//书籍名称查询
void LeGetBookInfoFroBName(LeBookLink BHead, char *BName) {
    LeBookLink p = BHead->Next;
    while (p) {
        //为0时一致
        int i = strcmp(BName, p->BName);
        if (i == 0) {
            printf("书籍ID：%d\t", p->BID);
            printf("书本名：%s\t", p->BName);
            printf("作者名：%s\t", p->BAName);
            printf("出版号：%ld\t", p->BPID);
            switch (p->BState) {
                case -1:
                    printf("书籍状态：不可用\n");
                    break;
                case 0:
                    printf("书籍状态：已借出\t");
                    printf("借书人ID：%d\n", p->SID);
                    break;
                case 1:
                    printf("书籍状态：可借出\n");
                    break;
            }
//            return;
        }
        p = p->Next;
    }

}

//书籍作者查询
void LeGetBookInfoFroBAName(LeBookLink BHead, char *BAName) {
    LeBookLink p = BHead->Next;
    while (p) {
        //为0时一致
        int i = strcmp(BAName, p->BAName);
        if (i == 0) {
            printf("书籍ID：%d\t", p->BID);
            printf("书本名：%s\t", p->BName);
            printf("作者名：%s\t", p->BAName);
            printf("出版号：%ld\t", p->BPID);
            switch (p->BState) {
                case -1:
                    printf("书籍状态：不可用\n");
                    break;
                case 0:
                    printf("书籍状态：已借出\t");
                    printf("借书人ID：%d\n", p->SID);
                    break;
                case 1:
                    printf("书籍状态：可借出\n");
                    break;
            }
//            return;
        }
        p = p->Next;
    }

}

//书籍书号查询
void LeGetBookInfoFroBPID(LeBookLink BHead, long BPID) {
    LeBookLink p = BHead->Next;
    while (p) {
        //为0时一致
        if (BPID == p->BPID) {
            printf("书籍ID：%d\t", p->BID);
            printf("书本名：%s\t", p->BName);
            printf("作者名：%s\t", p->BAName);
            printf("出版号：%ld\t", p->BPID);
            switch (p->BState) {
                case -1:
                    printf("书籍状态：不可用\n");
                    break;
                case 0:
                    printf("书籍状态：已借出\t");
                    printf("借书人ID：%d\n", p->SID);
                    break;
                case 1:
                    printf("书籍状态：可借出\n");
                    break;
            }
//            return;
        }
        p = p->Next;
    }

}

//书库书籍数量
void LeGetBookCount(LeBookLink BHead, int Flag) {
    /*
     *Flag == 0 查询已借书籍
     *Flag == 1 查询可借书籍
     *Flag == 2 查询全部书籍
     */
    LeBookLink p = BHead->Next;
    int s0 = 0, s1 = 0, All = 0;//借出, 可借，全部
    while (p) {
        if (p->BState == 0)s0++;
        else if (p->BState == 1)s1++;
        else All++;
        p = p->Next;
    }
    All = All + s0 + s1;
    switch (Flag) {
        case 0:
            printf("已借出书籍数量为%d\n", s0);
            break;
        case 1:
            printf("可借出书籍数量为%d\n", s1);
            break;
        default:
            printf("全部书籍数量为%d\n", All);
    }


}

//根据借书人ID查询借出的书籍
void LeGetBookCountFroSID(LeBookLink BHead, int SID) {
    LeBookLink p = BHead->Next;
    while (p) {
        if (SID == p->SID) {
            printf("书籍ID：%d\t", p->BID);
            printf("书本名：%s\t", p->BName);
            printf("作者名：%s\t", p->BAName);
            printf("出版号：%ld\t", p->BPID);
            switch (p->BState) {
                case -1:
                    printf("书籍状态：不可用\n");
                    break;
                case 0:
                    printf("书籍状态：已借出\t");
                    printf("借书人ID：%d\n", p->SID);
                    break;
                case 1:
                    printf("书籍状态：可借出\n");
                    break;
            }
//            return;
        }
        p = p->Next;
    }


}

//书籍ID删除书
void LeDelBookFroBID(LeBookLink BHead, LeStuLink SHead, int BID) {
    LeBookLink p = BHead->Next, pre = BHead;
    while (p) {
        if (BID == p->BID) {
            if (p->BState != 0) {
                pre->Next = p->Next;
                free(p);
                printf("删除成功,ID:%d\n", BID);
            } else {
                printf("书籍已被借出不可删除\n");
            }
            break;
        }
        pre = p;
        p = p->Next;
    }
    //将新的列表写入文件
    JsonWritFile(BHead->Next, SHead->Next);
}

//书名删除该书(所有同名书籍)
void LeDelBookFroBName(LeBookLink BHead, LeStuLink SHead, char *BName) {
    LeBookLink p = BHead->Next, pre = BHead, t;

    while (p) {
        if (strcmp(BName, p->BName) == 0) {
            if (p->BState != 0) {
                pre->Next = p->Next;
                t = p;
                printf("删除成功,书籍ID:%d\n", p->BID);
                p = p->Next;
                free(t);
                continue;
            } else {
                printf("书籍已被借出不可删除,书籍ID:%d\n", p->BID);
            }
        }
        pre = p;
        p = p->Next;
    }
    //将新的列表写入文件
    JsonWritFile(BHead->Next, SHead->Next);
}

//作者名删除书籍(所有该作者书籍)
void LeDelBookFroBAName(LeBookLink BHead, LeStuLink SHead, char *BAName) {
    LeBookLink p = BHead->Next, pre = BHead, t;

    while (p) {
        if (strcmp(BAName, p->BAName) == 0) {
            if (p->BState != 0) {
                pre->Next = p->Next;
                t = p;
                printf("删除成功,书籍ID:%d\n", p->BID);
                p = p->Next;
                free(t);
                continue;
            } else {
                printf("书籍已被借出不可删除,书籍ID:%d\n", p->BID);
            }
        }
        pre = p;
        p = p->Next;
    }
    //将新的列表写入文件
    JsonWritFile(BHead->Next, SHead->Next);
}

//书号删除书籍(所有该书籍)
void LeDelBookFroBPID(LeBookLink BHead, LeStuLink SHead, long BPID) {
    LeBookLink p = BHead->Next, pre = BHead, t;

    while (p) {
        if (BPID == p->BPID) {
            if (p->BState != 0) {
                pre->Next = p->Next;
                t = p;
                printf("删除成功,书籍ID:%d\n", p->BID);
                p = p->Next;
                free(t);
                continue;
            } else {
                printf("书籍已被借出不可删除,书籍ID:%d\n", p->BID);
            }
        }
        pre = p;
        p = p->Next;
    }
    //将新的列表写入文件
    JsonWritFile(BHead->Next, SHead->Next);
}

//办理借书手续
int LeBorrowBook(LeBookLink BHead, LeStuLink SHead, int BID, int SID) {
    LeStuLink StuP = SHead->Next;
    LeBookLink p = BHead->Next;

    while (StuP) {
        if (SID == StuP->SID) {
            break;
        }
        StuP = StuP->Next;
    }

    while (p && StuP) {
        if (BID == p->BID) {//找到这本书
            if (p->BState != 0) {//判断书籍是否被借出
                p->BState = 0;//修改书籍状态
                p->SID = StuP->SID;//修改书籍被借人
                StuP->SQuota -= 1;//扣除借书额度
                JsonWritFile(BHead->Next, SHead->Next);//将新的列表写入文件
                printf("借书完成！\n");
                return 1;
            } else {
                printf("书籍已被借出，借出人ID：%d\n", p->SID);
                return 0;
            }
        }
        p = p->Next;
    }
    printf("未找到该书籍，请重试\n");
    return -1;
}

//办理还书
int LeReturnBook(LeBookLink BHead, LeStuLink SHead, int BID, int E) {

    LeBookLink p = BHead->Next;
    while (p) {
        if (BID == p->BID) {//找到这本书
            if (E != 1)E = -1;
            p->BState = E;//修改书籍状态
            p->SID = 0;//修改书籍被借人
            JsonWritFile(BHead->Next, SHead->Next);//将新的列表写入文件
            printf("还书完成！\n");
            return 1;
        }
        p = p->Next;
    }
    printf("未找到该书籍，请重试\n");
    return -1;
}